home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / TextAction.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  130 lines

  1. /*
  2.  * @(#)TextAction.java    1.16 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.awt.event.ActionEvent;
  23. import java.util.Hashtable;
  24. import java.util.Enumeration;
  25. import com.sun.java.swing.Action;
  26. import com.sun.java.swing.AbstractAction;
  27. import com.sun.java.swing.KeyStroke;
  28.  
  29. /**
  30.  * An Action implementation useful for key bindings that are 
  31.  * shared across a number of different text components.  Because
  32.  * the action is shared, it must have a way of getting it's 
  33.  * target to act upon.  This class provides support to try and
  34.  * find a text component to operate on.  The preferred way of
  35.  * getting the component to act upon is through the ActionEvent
  36.  * that is received.  If the Object returned by getSource can
  37.  * be narrowed to a text component, it will be used.  If the
  38.  * action event is null or can't be narrowed, the last focused
  39.  * text component is tried.  This is determined by being
  40.  * used in conjunction with a JTextController which 
  41.  * arranges to share that information with a TextAction.
  42.  * <p>
  43.  * Warning: serialized objects of this class will not be compatible with
  44.  * future swing releases.  The current serialization support is appropriate
  45.  * for short term storage or RMI between Swing1.0 applications.  It will
  46.  * not be possible to load serialized Swing1.0 objects with future releases
  47.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  48.  * baseline for the serialized form of Swing objects.
  49.  *
  50.  * @author  Timothy Prinzing
  51.  * @version 1.16 04/09/98
  52.  */
  53. public abstract class TextAction extends AbstractAction {
  54.  
  55.     /**
  56.      * Creates a new JTextAction object.
  57.      *
  58.      * @param name the name of the action
  59.      */
  60.     public TextAction(String name) {
  61.     super(name);
  62.     }
  63.  
  64.     /**
  65.      * Determines the component to use for the action.
  66.      * This if fetched from the source of the ActionEvent
  67.      * if it's not null and can be narrowed.  Otherwise,
  68.      * the last focused component is used.
  69.      *
  70.      * @param e the ActionEvent
  71.      * @return the component
  72.      */
  73.     protected final JTextComponent getTextComponent(ActionEvent e) {
  74.     if (e != null) {
  75.         Object o = e.getSource();
  76.         if (o instanceof JTextComponent) {
  77.         return (JTextComponent) o;
  78.         }
  79.     }
  80.     return getFocusedComponent();
  81.     }
  82.     
  83.     /**
  84.      * Takes one list of 
  85.      * commands and augments it with another list
  86.      * of commands.  The second list is considered
  87.      * to be higher priority than the first list
  88.      * and commands with the same name will both lists
  89.      * will only have the dominate command found in the 
  90.      * second list in the returned list.
  91.      *
  92.      * @param list1 the first list, may be empty but not null
  93.      * @param list2 the second list, may be empty but not null
  94.      * @return the augmented list
  95.      */
  96.     public static final Action[] augmentList(Action[] list1, Action[] list2) {
  97.     Hashtable h = new Hashtable();
  98.     for (int i = 0; i < list1.length; i++) {
  99.         Action a = list1[i];
  100.         String value = (String)a.getValue(Action.NAME);
  101.         h.put((value!=null ? value:""), a);
  102.     }
  103.     for (int i = 0; i < list2.length; i++) {
  104.         Action a = list2[i];
  105.         String value = (String)a.getValue(Action.NAME);
  106.         h.put((value!=null ? value:""), a);
  107.     }
  108.     Action[] actions = new Action[h.size()];
  109.     int index = 0;
  110.         for (Enumeration e = h.elements() ; e.hasMoreElements() ;) {
  111.             actions[index++] = (Action) e.nextElement();
  112.         }
  113.     return actions;
  114.     }
  115.  
  116.     /**
  117.      * Fetches the text component that currently has focus.
  118.      * This allows actions to be shared across text components
  119.      * which is useful for key-bindings where a large set of
  120.      * actions are defined, but generally used the same way
  121.      * across many different components.
  122.      *
  123.      * @return the component
  124.      */
  125.     protected final JTextComponent getFocusedComponent() {
  126.     return JTextComponent.getFocusedComponent();
  127.     }
  128.  
  129. }
  130.